//
//  ViewController.m
//  UICollectionView
//
//  Created by wonders on 2024/12/2.
//

#import "ViewController.h"
#import "mainCollectionViewCell.h"
#define kScreen_Width [UIScreen mainScreen].bounds.size.width
#define kScreen_Height [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionView *collectionView1;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    [self setupView];
    self.dataSource = [NSMutableArray array];
        
    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"1"}];
    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"2"}];
    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"3"}];
    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"4"}];
    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"5"}];
//    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"6"}];
//    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"7"}];
//    [self.dataSource addObject:@{@"image": @"collectionView", @"title": @"8"}];
    
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.collectionView1];
    [self.view addSubview:self.tableView];
    [self layout];
}


#pragma mark -UICollectionView Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        NSInteger itemsPerPage = 4;
        return self.dataSource.count + (itemsPerPage - (self.dataSource.count % itemsPerPage)) % itemsPerPage;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        mainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCollectionViewCell" forIndexPath:indexPath];
        
        BOOL isEmpty = NO;
        if (indexPath.item >= self.dataSource.count) {
            isEmpty = YES;
        }
        
        if (!isEmpty) {
            NSDictionary *item = self.dataSource[indexPath.item];
            cell.icon.image = [UIImage imageNamed:item[@"image"]];
            cell.title.text = item[@"title"];
        } else {
            cell.icon.image = nil;
            cell.title.text = @"";
        }
        return cell;
    }
    





#pragma mark -collectionView layout
- (void)layout {
    CGFloat collectionViewX = 15;
    CGFloat collectionViewY = 100;
    CGFloat collectionViewWidth = kScreen_Width - 2 * collectionViewX;
    CGFloat collectionViewHeight = 100;
    CGRect collectionViewFrame = CGRectMake(collectionViewX, collectionViewY, collectionViewWidth, collectionViewHeight);
    _collectionView.frame = collectionViewFrame;
    
    
    
}
#pragma mark -layUI
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreen_Width-30)/4, 60);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = YES;
        if (@available (iOS 13.0, *)){
            _collectionView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
        }
        _collectionView.contentInset = UIEdgeInsetsMake(10, 0, 15, 0);
        [_collectionView registerClass:[mainCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([mainCollectionViewCell class])];
    }
    return _collectionView;
}







@end
